package top.lixb.libcommon.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import top.lixb.libcommon.R;

public abstract class RefreshableFragment<T extends ViewDataBinding> extends BaseFragment {


    protected SmartRefreshLayout mSrl;
    protected NestedScrollView mRootScroll;
    protected T mRefreshContentBinding;


    @Override
    protected void initView(View view) {
        mSrl = view.findViewById(R.id.srl);
        mRootScroll = view.findViewById(R.id.scrollroot);
        mSrl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData(true);
            }
        });
        mRefreshContentBinding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), initRefreshLayout(), mRootScroll, true);
        init();
    }
    protected abstract void init();
    protected abstract int initRefreshLayout();
    protected abstract void getData(boolean refresh);

    protected void finishGetData() {
        if (null != mSrl) {
            mSrl.finishLoadMore(2000);
        }

        if (null != mSrl) {
            mSrl.finishRefresh(2000);
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_base_refresh;
    }



}
